package rr_test_client;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;

public class TCMainWindow {
	
	Text scoreTxt;
	Display display;
	TestClient testClient = new TestClient(this);
	int playerId = -1;
	StyledText styledText;
	StyledText styledText_1;
	Shell shlRhythmRouletteTest;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TCMainWindow window = new TCMainWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		shlRhythmRouletteTest = new Shell();
		shlRhythmRouletteTest.setBackground(SWTResourceManager.getColor(0, 0, 128));
		shlRhythmRouletteTest.setSize(950, 500);
		shlRhythmRouletteTest.setText("Rhythm Roulette Test Client");
		
		styledText = new StyledText(shlRhythmRouletteTest, SWT.BORDER);
		styledText.setBottomMargin(10);
		styledText.setLeftMargin(10);
		styledText.setTopMargin(10);
		styledText.setRightMargin(10);
		styledText.setMarginColor(SWTResourceManager.getColor(0, 0, 0));
		styledText.setEditable(false);
		styledText.setForeground(SWTResourceManager.getColor(255, 20, 147));
		styledText.setFont(SWTResourceManager.getFont("Fixedsys", 9, SWT.NORMAL));
		styledText.setBackground(SWTResourceManager.getColor(0, 0, 0));
		styledText.setBounds(0, 0, 622, 455);
		styledText.setWordWrap(true);
		Button playRequestBtn = new Button(shlRhythmRouletteTest, SWT.NONE);		
		playRequestBtn.addSelectionListener(new SelectionAdapter() {
			// playRequestBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String playRequestMsg = testClient.messageUtil.makePlayRequestMsg();
				testClient.sendText(playRequestMsg);
			}
		});
		playRequestBtn.setBounds(702, 10, 162, 30);
		playRequestBtn.setText("Start A New Game");
		
		Button resultBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		resultBtn.addSelectionListener(new SelectionAdapter() {
			// resutlBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String score;
				score = scoreTxt.getText();
				scoreTxt.setText("");
				String resultMsg = "<result><sender>" + testClient.playerId + "</sender><score>" + score + "</score></result>";
				testClient.sendText(resultMsg);
			}
		});
		resultBtn.setBounds(702, 117, 162, 30);
		resultBtn.setText("Submit Score");
		
		Button quitRequestBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		quitRequestBtn.addSelectionListener(new SelectionAdapter() {
			// quitRequestBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String quitRequestMsg = testClient.messageUtil.makeQuitRequestMsg();
				testClient.sendText(quitRequestMsg);
			}
		});
		quitRequestBtn.setBounds(702, 415, 162, 30);
		quitRequestBtn.setText("Log Off");
		
		scoreTxt = new Text(shlRhythmRouletteTest, SWT.BORDER);
		scoreTxt.setBounds(702, 71, 162, 26);
		
		styledText_1 = new StyledText(shlRhythmRouletteTest, SWT.BORDER);
		styledText_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		styledText_1.setBottomMargin(3);
		styledText_1.setTopMargin(3);
		styledText_1.setRightMargin(3);
		styledText_1.setLeftMargin(3);
		styledText_1.setFont(SWTResourceManager.getFont("Fixedsys", 9, SWT.NORMAL));
		styledText_1.setBounds(628, 178, 294, 157);
		styledText_1.setWordWrap(true);
		
		Button sendMsgBtn = new Button(shlRhythmRouletteTest, SWT.NONE);
		sendMsgBtn.addSelectionListener(new SelectionAdapter() {
			// sendMsgBtn event handler
			public void widgetSelected(SelectionEvent e) {
				String message = "";
				message = styledText_1.getText();
				styledText_1.setText("");
				testClient.sendText(message);
			}
		});
		
		sendMsgBtn.setBounds(702, 351, 162, 30);
		sendMsgBtn.setText("Send Message");
		shlRhythmRouletteTest.open();
		shlRhythmRouletteTest.layout();
				
		while (!shlRhythmRouletteTest.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}
